var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();
Page({
  data: {
    userInfo:{},
    navH:0,
    memberList:[],
    curMember:{},
    price:9.9,
    active:0,
    privileges:[],
    memberInfo:{}
  },
  onLoad: function (options) {
    
    this.setData({

      navH: app.globalData.navHeight

    });
    this.getMemberInfo();
    this.getMemberCardList();
  },
  getMemberInfo() {
    let _this = this;
    util.request(api.MemberInfo).then(res=>{
        console.log(111,res)
        _this.setData({
          memberInfo:res.result
        })
        app.globalData.cartNum = res.result.shoppingCartCount
    }).catch(err=>{
      console.log(err)
    })
  },
  getMemberCardList:function(){
    let _this = this;

    util.request(api.memberCardList).then(res=>{
      console.log(111,res)
      _this.setData({
        memberList:res.result,
        curMember:res.result[0],
      })
      console.log(1111,res.result[0].privileges)
      if(res.result[0].privileges){
        _this.setData({
          privileges:res.result[0].privileges,
        })
      }
    }).catch(err=>{
      console.log(err)
    })
  },
  choicePrice:function(e){
    let _this = this;
    this.setData({
      active:e.currentTarget.dataset.sel,
      curMember:this.data.memberList[e.currentTarget.dataset.sel]
    })
    if(this.data.memberList[e.currentTarget.dataset.sel].privileges){
      _this.setData({
        privileges:this.data.memberList[e.currentTarget.dataset.sel].privileges,
      })
    }else{
      _this.setData({
        privileges:[],
      })
    }
  },
  buyMember:function(){
    let _this = this;
    util.request(api.memberCardPay,{id:_this.data.curMember.id},'POST').then(res=>{
      console.log(res)

      let payParam = res.result;
      wx.requestPayment({
        'timeStamp': payParam.timeStamp,
        'nonceStr': payParam.nonceStr,
        'package': payParam.packageValue,
        'signType': payParam.signType,
        'paySign': payParam.paySign,
        'success': function (res) {
          wx.showToast({
              title: '支付成功'
          });
        },
        'fail': function (res) {
          wx.showToast({
              title: '支付失败'
          });
        }
      })

    }).catch(err=>{
      console.log(err)
    })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})